﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace Shuba.Web.master
{
    public partial class flag_freeze_add : basePage
    {
        string msg = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Form.DefaultFocus = txtFlag.ClientID;
            this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("FLAG_FREEZE");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("FLAG_FREEZE");
                //RegVal1.ValidationExpression = "^[\\d\\s]+$";//"^[0-9\\s]+$";

                if (Request.QueryString["a"] == "ed")
                {
                    BindData();
                }
            }
        }

        private void BindData()
        {
            Shuba.Business.FlagFreeze branch = new Shuba.Business.FlagFreeze();
            Shuba.Model.FlagFreezeInfo info;

            string Param = Request.QueryString["id"];
            string[] ParamID = Param.Split('~');
            string flag = ParamID[0].ToString().Trim();
            string field = ParamID[1].ToString().Trim();
            info = branch.Get(Convert.ToInt16(flag), field);

            txtFlag.Text = info.FLAG.ToString();
            bool fieldChecked = false;
            switch (info.FIELD.ToUpper())
            {
                case "PROSTYPE":
                    chkbxProspect.Checked = true;
                    if (info.ENABLED == "1")
                    {
                        rb_prospect.SelectedValue = info.ENABLED;
                    }
                    break;
                case "DEBTYPE":
                    chkDebtor.Checked = true;
                    if (info.ENABLED == "1")
                    {
                        rb_debtor.SelectedValue = info.ENABLED;
                    }
                    break;
                case "CHANNEL":
                    chkChannel.Checked = true;
                    if (info.ENABLED == "1")
                    {
                        rb_channel.SelectedValue = info.ENABLED;
                    }
                    break;
                case "EXTERNAL":
                    chkExternal.Checked = true;
                    if (info.ENABLED == "1")
                    {
                        rb_external.SelectedValue = info.ENABLED;
                    }
                    break;
                case "REFERENSI":
                    chkRefrensi.Checked = true;
                    if (info.ENABLED == "1")
                    {
                        rb_refrensi.SelectedValue = info.ENABLED;
                    }
                    break;
            }

            if (info.IS_ACTIVE == "0") btnSubmit.Text = "Activate"; else btnSubmit.Text = "Submit";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Shuba.Business.FlagFreeze bl = new Shuba.Business.FlagFreeze();
            Shuba.Model.FlagFreezeInfo bi = new Shuba.Model.FlagFreezeInfo();

            int successcount = 0;
            int failedcount = 0;

            string ActType = "";
            string[] field = getArray();
            string[] enabled = getArrayEnabled();

            for (int i = 0; i < field.Length; i++)
            {
                if (field[i] == null)
                {
                    continue;
                }
                else
                {
                    if (Request.QueryString["a"] != "ed")
                    {
                        bi = bl.Get(Convert.ToInt16(txtFlag.Text), field[i]);
                        if (bi.FLAG == 0 && bi.FIELD == null)
                        {
                            ActType = "INSERT";
                        }
                        else
                        {
                            failedcount++;
                            continue;
                            //msg = "This Flag Already Exist";
                            //Alert(msg);
                        }
                    }
                    else
                    {
                        ActType = "UPDATE";
                    }

                    if (msg == "")
                    {
                        bi.FLAG = Convert.ToInt16(txtFlag.Text);
                        bi.FIELD = field[i];
                        bi.ENABLED = enabled[i];
                        bi.ENTRY_BY = HttpContext.Current.User.Identity.Name;
                        bi.ENTRY_DATE = ProcessingDate();
                        bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
                        bi.UPDATE_DATE = ProcessingDate();
                        bi.ACT_TYPE = ActType;
                        bi.IS_ACTIVE = "1";
                        try
                        {
                            if (bl.Save(bi) > 0)
                            {
                                successcount++;
                                AddLog(ActType, ActType + " FLAG FREEZE");
                            }
                            else
                            {
                                failedcount++;
                            }
                        }
                        catch (Exception ex)
                        {
                            ErrBox.Visible = true;
                            ErrMsg.Text = ex.Message;
                        }
                    }
                }
            }
            if (successcount > 0 || failedcount > 0)
            {
                msg = "Save Notification\\n\\nSuccessfully Save : " + successcount + " Record(s)\\n\\nFailed to Save : " + failedcount + " Record(s)";
            }
            //msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
            AlertAndRedirect(msg, "flag_freeze_list.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("flag_freeze_list.aspx");
        }

        protected string[] getArray()
        {
            string[] arrayField = new string[5];

            if (chkbxProspect.Checked)
            {
                arrayField[0] = "PROSTYPE";
            }
            if (chkDebtor.Checked)
            {
                arrayField[1] = "DEBTYPE";
            }
            if (chkChannel.Checked)
            {
                arrayField[2] = "CHANNEL";
            }
            if (chkExternal.Checked)
            {
                arrayField[3] = "EXTERNAL";
            }
            if (chkRefrensi.Checked)
            {
                arrayField[4] = "REFERENSI";
            }

            return arrayField;
        }

        protected string[] getArrayEnabled()
        {
            string[] arrayEnable = new string[5];

            if (rb_prospect.SelectedValue != null)
            {
                arrayEnable[0] = rb_prospect.SelectedValue == null ? " " : rb_prospect.SelectedValue;
            }
            if (rb_debtor.SelectedValue != null)
            {
                arrayEnable[1] = rb_debtor.SelectedValue == null ? " " : rb_debtor.SelectedValue;
            }
            if (rb_channel.SelectedValue != null)
            {
                arrayEnable[2] = rb_channel.SelectedValue == null ? " " : rb_channel.SelectedValue;
            }
            if (rb_external.SelectedValue != null)
            {
                arrayEnable[3] = rb_external.SelectedValue == null ? " " : rb_external.SelectedValue;
            }
            if (rb_refrensi.SelectedValue != null)
            {
                arrayEnable[4] = rb_refrensi.SelectedValue == null ? " " : rb_refrensi.SelectedValue;
            }

            return arrayEnable;
        }

    }
}